#Writen by Koretsis Georgios
#Date 22/06/2014 , Thessaloniki
#Python 3.4.1
#Use at your own risk
#Licenced under GNU GPL v3


i=1
part=30000   #Define max part size in bytes
enoumlines=0
enoumlinesold=0
linesprog=0
enoumsize=0
enoumsizeprev=0
sizeprog=0
sumsizeprog=0
initString=""
Z=""
F=""
first = False

def subprog():
    
    global initString
    global Z
    global F
    global first
    
    if enoumlines<=8:

        initString=initString+line

    if first:
        
        j=i-1
        f1 = open(str(j)+".txt","a")
        f1.write("G0 Z20.\nM30\n%")
        f1.close()
        
        f1= open(str(i)+".txt","a")
        f1.write(initString)
        f1.write("M3\nG1 Z"+Z+" F"+F+"\n")
        f1.close()
        first=False


    if line.find("Z") != -1 :
        a=line.find("Z")
        if line.find(" ",a) != -1 :
            b=line.find(" ",a)
            Z=line[a+1:b]
        elif line.find("\n",a) != -1 :
            b=line.find("\n",a)
            Z=line[a+1:b]

    if line.find("F") != -1 :
        a=line.find("F")           
        if line.find(" ",a) != -1 :
            b=line.find(" ",a)
            F=line[a+1:b]               
        elif line.find("\n",a) != -1:
            b=line.find("\n",a)
            F=line[a+1:b]


    f1= open(str(i)+".txt","a")
    f1.write(line)
    f1.close()


f = open('input.txt','r')


for line in iter(f):

    enoumlines += 1
    enoumsizeprev=enoumsize
    enoumsize= enoumsize + len(line)

    if enoumsize>part*i:

        linesprog=enoumlines-enoumlinesold
        enoumlinesold=enoumlines
        sizeprog=enoumsizeprev - sumsizeprog
        f3=open("data.txt","a")
        f3.write(str(i)+" text file: "+str(linesprog)+" lines and "+str(sizeprog)+" bytes")
        f3.write("\n")
        f3.close
        sumsizeprog=sumsizeprog + sizeprog

        
        first = True
        i += 1

    subprog()




f.close()


sizeprog=enoumsize - sumsizeprog
linesprog=enoumlines-enoumlinesold
enoumlinesold=enoumlines
f3=open("data.txt","a")
f3.write(str(i)+" text file: "+str(linesprog)+" lines and "+str(sizeprog)+" bytes")
f3.write("\n")
f3.write("total lines readed: "+str(enoumlines)+"\ntotal bytes readed: "+str(enoumsize))
f3.close()
